package com.elitel.frame.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.CodeService;
import com.elitel.frame.main.dao.CodeSystemMapper;
import com.elitel.frame.main.dao.CodeTypeMapper;
import com.elitel.frame.main.entity.CodeSystem;
import com.elitel.frame.main.entity.CodeType;
import com.elitel.frame.main.entity.ext.CodeSys;
import com.elitel.frame.main.entity.vo.PageResponseVo;

/**
 * 
 * @author dingdaifu
 *
 */
@Service
public class CodeServiceImpl implements CodeService {
	
	@Autowired
	private CodeSystemMapper codeSystemMapper;
	@Autowired
	private CodeTypeMapper codeTypeMapper;

	@Override
	public List<CodeType> getCodeTree() {
		List<CodeType> list = codeTypeMapper.getCodeTree();
		return list;
	}

	@Override
	public List<CodeSystem> getCodeSystem(String sysId) {
		int id = Integer.parseInt(sysId);
		List<CodeSystem> list = codeSystemMapper.getCodeSystem(id);
		return list;
	}

	@Override
	public String insetCodeSystem(CodeSystem codeSystem) {
		long time = System.currentTimeMillis();
		codeSystem.setId(time);
		int a = codeSystemMapper.insertCodeSystem(codeSystem);
		String message = "fail";
		if(a>0){
			message = "success";
		}
		return message;
	}

	@Override
	public String deleteCodeSystem(String id) {
		long ids = Long.parseLong(id);
		int a = codeSystemMapper.delectCodeSystem(ids);
		String message = "fail";
		if(a>0){
			message = "success";
		}
		return message;
	}

	@Override
	public String updateCodeSystem(CodeSystem codeSystem) {
		int a = codeSystemMapper.updateCodeSystem(codeSystem);
		String message = "fail";
		if(a>0){
			message = "success";
		}
		return message;
	}

	@Override
	public CodeSystem selectCodeOne(String id) {
		long ids = Long.parseLong(id);
		CodeSystem codeSystem = codeSystemMapper.selectCodeOne(ids);
		return codeSystem;
	}

	@Override
	public List<CodeSystem> selectCodeSystem(String sysId) {
		int sysIds = Integer.parseInt(sysId);
		List<CodeSystem> list = codeSystemMapper.selectCodeSystem(sysIds);
		return list;
	}

	@Override
	public List<CodeType> selectCodeType() {
		List<CodeType> list = null;
		try {
			list = codeTypeMapper.getCodeTree();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public PageResponseVo<CodeSystem> getFenYe(String pageNo, String pageSize,
			String sysId) {
		int a = Integer.parseInt(pageNo)-1;
		int b = Integer.parseInt(pageSize);
		int rowStart = a*b+1;
		int rowEnd = Integer.parseInt(pageNo)*Integer.parseInt(pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rowStart", rowStart);
		map.put("rowEnd",rowEnd);
		map.put("sysId", sysId);
		List<CodeSystem> list =  codeSystemMapper.getFenYe(map);
		int count = codeSystemMapper.getCount(Integer.parseInt(sysId));
		PageResponseVo<CodeSystem> pageResponseVo = new PageResponseVo<CodeSystem>();
		pageResponseVo.setRows(list);
		pageResponseVo.setTotal(count);
		return pageResponseVo;
	}

	@Override
	public String insertCodeType(CodeType codeType) {
		int b = codeTypeMapper.selectMax();
		codeType.setId(b+1);
		int a = codeTypeMapper.insert(codeType);
		String message = "fail";
		if (a>0) {
			message = "success";
		}
		return message;
	}

	@Override
	public CodeType selectCodeTypeOne(String id) {
		CodeType codeType = null;
		try {
			 codeType = codeTypeMapper.selectCodeTypeOne(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return codeType;
	}

	@Override
	public String deleteCodeType(int parseInt) {
		int a = codeTypeMapper.deleteCodeType(parseInt);
		String message = "fail";
		if (a>0) {
			codeSystemMapper.delectSysBySysId(parseInt);
			message = "success";
		}
		return message;
	}

	@Override
	public String updateCodeType(CodeType codeType) {
			String message = "fail";
		try {
			int a = codeTypeMapper.updateCodeType(codeType);
			if (a>0) {
				message = "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}

	@Override
	public PageResponseVo<CodeSystem> getAllCode(String sysId) {
		PageResponseVo<CodeSystem> pageResponseVo = new PageResponseVo<CodeSystem>();
		List<CodeSystem> list = codeSystemMapper.getAllCode(sysId);
		int count = codeSystemMapper.getCount(Integer.parseInt(sysId));
		pageResponseVo.setRows(list);
		pageResponseVo.setTotal(count);
		return pageResponseVo;
	}

	@Override
	public CodeSystem getCodeOne(String id) {
		CodeSystem codeSys = codeSystemMapper.getCodeOne(id);
		return codeSys;
	}

}
